package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.BackResponseEntity;
import com.example.demo.entity.Post;
import com.example.demo.entity.PostComment;
import com.example.demo.entity.requestJson.DeleteReq;
import com.example.demo.entity.requestJson.PostOnelevelCommentReq;
import com.example.demo.entity.requestJson.PostReplyTwoCommentReq;
import com.example.demo.entity.requestJson.PostTwoLevelCommentReq;
import com.example.demo.entity.responseJson.ListResult;
import com.example.demo.entity.responseJson.PostOnelevelCommentRes;
import com.example.demo.entity.responseJson.PostTwolevelCommentRes;
import com.example.demo.enums.ErrCodeEnum;
import com.example.demo.mapper.PostCommentMapper;
import com.example.demo.mapper.PostMapper;
import com.example.demo.service.PostCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 帖子评论表(PostComment)Service层
 *
 * @author 杨丽辉
 * @since 2021-03-17 11:23:43
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class PostCommentServiceImpl implements PostCommentService {
        @Autowired
        private PostCommentMapper postCommentMapper;
        @Autowired
        private PostMapper postMapper;


 /**
     * 通过ID和其他条件查询单条数据
     *
     * @param postComment 实例对象
     * @return 实例对象
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public PostComment queryPostCommentLimit1(PostComment postComment){
     return  postCommentMapper.queryPostCommentLimit1( postComment);
     }
     
 /**
     * 查询指定行数据
     * @param postComment 实例对象
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   List<PostComment> queryAllPostCommentByLimit( PostComment postComment, Integer offset,  Integer limit){
   return postCommentMapper.queryAllPostCommentByLimit(postComment, offset,limit);
  }

  /**
     * 查询数据条数
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int countAllPostComment(PostComment postComment){
   return postCommentMapper.countAllPostComment( postComment);
   }
   /**
     * 通过实体作为筛选条件查询
     *
     * @param postComment 实例对象
     * @return 对象列表
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public List<PostComment> queryAllPostComment(PostComment postComment){
   return postCommentMapper.queryAllPostComment(postComment);
   }
  /**
     * 新增数据
     *
     * @param postComment 实例对象
     * @return 影响行数
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int insertPostComment(PostComment postComment){
    return  postCommentMapper.insertPostComment( postComment);
    }
  /**
     * 修改数据
     *
     * @param postComment 实例对象
     * @return 影响行数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
     public  int updatePostComment(PostComment postComment){
         return postCommentMapper.updatePostComment( postComment);
     }

    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 影响行数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int deletePostCommentById(String id){
   return  postCommentMapper.deletePostCommentById( id);
   }
     /**
     * 通过主键列表删除，列表长度不能为0
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int deletePostCommentByIds( List<String> list){
    return  postCommentMapper.deletePostCommentByIds(  list);
  }
    
       /**
     * 通过主键修改实体列表，列表长度不能为0，注意：当实体属性为null时，对应的列也会别更新为null
     * @param list 集合
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public int updatePostCommentBatch( List<PostComment> list){
   return  postCommentMapper.updatePostCommentBatch(list);
   }
    /**
     * 批量其他列,除主键以外的列，列表长度不能为0,字段是空的就置为null
     * @param list 集合
     * @return 条数
     */
      @Override
  @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
 public   int insertPostCommentBatch( List<PostComment> list){
 return postCommentMapper.insertPostCommentBatch( list);
 }
      /**
     * 通过实体非空属性删除
     * @param postComment  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public   int deletePostCommentByEntity( PostComment postComment){
  return  postCommentMapper.deletePostCommentByEntity(  postComment);
  }
    /**
     * 通过实体不为空的属性作为筛选条件查询列表
     * @param postComment  实体
     * @return list
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  List<PostComment> listPostCommentByEntity(PostComment postComment){
   return postCommentMapper.listPostCommentByEntity( postComment);
   }

 /**
     * 新增实体属性不为null的列
     * @param postComment  实体
     * @return 条数
     */
       @Override
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
   public  int insertPostCommentSelect( PostComment postComment){
      return postCommentMapper.insertPostCommentSelect( postComment);

   }
   
      /**
     * 批量新增新增实体属性不为null的列
     * @param list  实体
     * @return 条数
     */
        @Override
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
  public  int insertPostCommentListSelect( List<PostComment> list){

     return postCommentMapper.insertPostCommentListSelect( list);
      }
     /**
     * 通过实体不为空的属性作为筛选条件查询单个
     * @param postComment  实体
     * @return 单个
     */
     @Override
   @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public  PostComment getPostCommentByEntity(PostComment postComment){
     return  postCommentMapper.getPostCommentByEntity(postComment);
    }

    /**
     * 发布帖子一级评论
     * */
    @Override
    public BackResponseEntity<Object> putOneLevelComment(PostOnelevelCommentReq postOnelevelCommentReq){
        String userId = null;
        String postId = null;
        String content = null;

        if(!ObjectUtils.isEmpty(postOnelevelCommentReq.getUserId())){
            userId = postOnelevelCommentReq.getUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postOnelevelCommentReq.getPostId())){
            postId = postOnelevelCommentReq.getPostId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postOnelevelCommentReq.getContent())){
            content = postOnelevelCommentReq.getContent()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(postId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "参数缺失");
        }

        PostComment postComment = new PostComment();
        postComment.setUserId(userId);
        postComment.setPostId(postId);
        postComment.setContent(content);
        postComment.setCommentLevel(1);
        postComment.setCreateTime(LocalDateTime.now());

        int i = postCommentMapper.insert(postComment);

        if(i > 0){
            Post post = new Post();
            int count = postMapper.countcommentCount(Wrappers.<Post>query().eq("post_id", postId)) +1;
            post.setPostId(postId);
            post.setCommentCount(count);

            postMapper.updatePost(post);

            return BackResponseEntity.success(ErrCodeEnum.unknown_1022, "发表一级评论成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1023, "发表一级评论失败");
        }
    }

    /**
     *查询帖子一级评论
     * 传postId
    */
    @Override
    public BackResponseEntity<ListResult<List<PostOnelevelCommentRes>>> queryPostOneComment(DeleteReq deleteReq){

        List<PostOnelevelCommentRes> list = postCommentMapper.queryponelevelComment(Wrappers.<PostComment>query().eq("post_id",deleteReq.getId())
                                                                                                                 .eq("comment_level",1)
                                                                                                                 .orderByDesc("c.create_time"));
        int cunt = postCommentMapper.countOneComment(Wrappers.<PostComment>query().eq("post_id",deleteReq.getId())
                .eq("comment_level",1));
        ListResult<List<PostOnelevelCommentRes>> listResult = new ListResult(cunt,list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0,listResult, "");
    }

    /**
     * 回复一级评论
     * */
    @Override
    public BackResponseEntity<Object> putTwoLevelComment(PostTwoLevelCommentReq postTwoLevelCommentReq){
        String userId = null;
        String postId = null;
        String parentCommentId = null;
        String parentCommentUserId = null;
        String content = null;

        if(!ObjectUtils.isEmpty(postTwoLevelCommentReq.getUserId())){
            userId = postTwoLevelCommentReq.getUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postTwoLevelCommentReq.getPostId())){
            postId = postTwoLevelCommentReq.getPostId()
                    .trim();
        }

        if(!ObjectUtils.isEmpty(postTwoLevelCommentReq.getParentCommentId())){
            parentCommentId = postTwoLevelCommentReq.getParentCommentId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postTwoLevelCommentReq.getParentCommentUserId())){
            parentCommentUserId = postTwoLevelCommentReq.getParentCommentUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postTwoLevelCommentReq.getContent())){
            content = postTwoLevelCommentReq.getContent()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(postId) ||
                ObjectUtils.isEmpty(parentCommentId) || ObjectUtils.isEmpty(parentCommentUserId) || ObjectUtils.isEmpty(content)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        PostComment postComment = new PostComment();
        postComment.setUserId(userId);
        postComment.setPostId(postId);
        postComment.setParentCommentId(parentCommentId);
        postComment.setParentCommentUserId(parentCommentUserId);
        postComment.setContent(content);
        postComment.setCommentLevel(2);
        postComment.setCreateTime(LocalDateTime.now());

        int i = postCommentMapper.insert(postComment);

        if(i > 0){
            Post post = new Post();

            int count = postMapper.countcommentCount(Wrappers.<Post>query().eq("post_id", postId)) +1;
            post.setPostId(postId);
            post.setCommentCount(count);

            postMapper.updatePost(post);

            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "回复一级评论成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1025, "回复一级评论失败");
        }
    }

    /**
     * 查询二级评论
     * 传父评论id
     * */
    @Override
    public BackResponseEntity<ListResult<List<PostTwolevelCommentRes>>> queryTwoComment(DeleteReq deleteReq){
        String parentCommentId = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            parentCommentId = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(parentCommentId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        List<PostTwolevelCommentRes> list = postCommentMapper.queryptwoelevelComment(Wrappers.<PostComment>query().eq("parent_comment_id",deleteReq.getId())
                .eq("comment_level",2)
                .orderByDesc("c.create_time"));
        int cunt = postCommentMapper.countposttwoComment(Wrappers.<PostComment>query().eq("parent_comment_id",deleteReq.getId())
                .eq("comment_level",2));
        ListResult<List<PostTwolevelCommentRes>> listResult = new ListResult(cunt,list);

        return BackResponseEntity.success(ErrCodeEnum.unknown_0,listResult, "");
    }

    /**
     * 回复二级评论
     * */
    @Override
    public BackResponseEntity<Object> replyTwoComment(PostReplyTwoCommentReq postReplyTwoCommentReq){
        String userId = null;
        String postId = null;
        String parentCommentId = null;
        String parentCommentUserId = null;
        String replyCommentId = null;
        String replyCommentUserId = null;
        String content = null;

        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getUserId())){
            userId = postReplyTwoCommentReq.getUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getPostId())){
            postId = postReplyTwoCommentReq.getPostId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getParentCommentId())){
            parentCommentId = postReplyTwoCommentReq.getParentCommentId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getParentCommentUserId())){
            parentCommentUserId  = postReplyTwoCommentReq.getParentCommentUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getReplyCommentId())){
            replyCommentId = postReplyTwoCommentReq.getReplyCommentId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getReplyCommentUserId())){
            replyCommentUserId = postReplyTwoCommentReq.getReplyCommentUserId()
                    .trim();
        }
        if(!ObjectUtils.isEmpty(postReplyTwoCommentReq.getContent())){
            content = postReplyTwoCommentReq.getContent()
                    .trim();
        }

        if(ObjectUtils.isEmpty(userId) || ObjectUtils.isEmpty(postId) || ObjectUtils.isEmpty(parentCommentId) || ObjectUtils.isEmpty(parentCommentUserId)
                || ObjectUtils.isEmpty(replyCommentId) || ObjectUtils.isEmpty(replyCommentUserId)|| ObjectUtils.isEmpty(content)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        PostComment postComment = new PostComment();
        postComment.setUserId(userId);
        postComment.setPostId(postId);
        postComment.setParentCommentId(parentCommentId);
        postComment.setParentCommentUserId(parentCommentUserId);
        postComment.setReplyCommentId(replyCommentId);
        postComment.setReplyCommentUserId(replyCommentUserId);
        postComment.setContent(content);
        postComment.setCommentLevel(2);
        postComment.setCreateTime(LocalDateTime.now());

        int i = postCommentMapper.insert(postComment);

        if(i > 0){
            Post post = new Post();

            int count = postMapper.countcommentCount(Wrappers.<Post>query().eq("post_id", postId)) +1;
            post.setPostId(postId);
            post.setCommentCount(count);

            postMapper.updatePost(post);

            return BackResponseEntity.success(ErrCodeEnum.unknown_0, "回复二级评论成功");
        }else {
            return BackResponseEntity.success(ErrCodeEnum.unknown_1024, "回复二级评论失败");
        }
    }
    /**
     * 查询我评论的
     * */
    @Override
    public BackResponseEntity<List<PostTwolevelCommentRes>> querymyComment(DeleteReq deleteReq){
        String userId = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            userId = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        List<PostTwolevelCommentRes> list = postCommentMapper.querymyComment(Wrappers.<PostComment>query().eq("user_id",userId));

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, list,"查询我的评论成功");
    }

    /**
     * 查询评论我的
     * */
    @Override
    public BackResponseEntity<List<PostTwolevelCommentRes>> queryreplymyComment(DeleteReq deleteReq){
        String userId = null;

        if(!ObjectUtils.isEmpty(deleteReq.getId())){
            userId = deleteReq.getId()
                    .trim();
        }
        if(ObjectUtils.isEmpty(userId)){
            return BackResponseEntity.success(ErrCodeEnum.unknown_4006, "参数缺失");
        }

        List<PostTwolevelCommentRes> list = postCommentMapper.queryreplymyComment(Wrappers.<PostComment>query().eq("reply_comment_user_id",' ').eq("parent_comment_user_id",userId)
                                                                                                               .or().eq("reply_comment_user_id",userId));

        return BackResponseEntity.success(ErrCodeEnum.unknown_0, list,"查询评论我的成功");
    }
}

